#include "WordSegFolder.h"
#include "WordDic.h"

#include "common.h"



void WordsMap(char *str,map<string,int>mapword[],int map_nodes)
{
	FindMapWord(str,mapword,map_nodes);
}

void WordsMapStr(char *str,map<string,int>&mailmap)
{
	FindMapWordStr(str,mailmap);
}

void SegmentSentence(char *str,map<string,int>mapword[],int map_nodes)
{
	int ini_len=strlen(str);
	char * s1=(char *)malloc(ini_len+1);
	strcpy(s1,str);                                 //the Parameter copy the Temporary variable s1

	char *word_buf=(char *)malloc(ini_len);
	while(strlen(s1)!=0)                            //string is not null
	{
		memset(word_buf,0,ini_len);                 //initializion 0
		int len = strlen(s1); 
		
		if (len > MaxWordLength)                    //length of string > MaxWordLength 
		{
			len = MaxWordLength; 
		}
		
		strncpy(word_buf,s1+strlen(s1)-len,len);    //Take the final len bytes's word
		int n = WordDic.FindWord(word_buf);         // find word_buf word in WordDic	
		while(len > 3 && n == 0)                    //not find word_buf and len >3,one Chinese characters has three Byte
		{
			len -= 3; 
			memset(word_buf,0,ini_len);
			strncpy(word_buf,s1+strlen(s1)-len,len);//if the not find,then remove the first Chinese characters of the word
			n = WordDic.FindWord(word_buf);         //find word_buf word in WordDic	
		}	
		/*
		*if the word_buf can find in WordDic, then insert the word_buf into the map_nodes map
		*/
		if (n==1)
		{
			WordsMap(word_buf,mapword,map_nodes);
		}
		strncpy(word_buf,s1,strlen(s1)-len);        //discard the lastest len char
		
		if((strlen(s1)-len)!=0)
		{
			strcpy(s1,word_buf);
		}
		else
		{	
			memset(s1,0,strlen(s1));
		}
	}
	free(s1);
	free(word_buf);
}

void SegmentSentenceStr(char *str,map<string,int>&mailmap)
{
	int ini_len=strlen(str);
	char * s1=(char *)malloc(ini_len+1);
	strcpy(s1,str);                                 //the Parameter copy the Temporary variable s1

	char *word_buf=(char *)malloc(ini_len);
	while(strlen(s1)!=0)                            //string is not null
	{
		memset(word_buf,0,ini_len);                 //initializion 0
		int len = strlen(s1); 
		
		if (len > MaxWordLength)                    //length of string > MaxWordLength 
		{
			len = MaxWordLength; 
		}
		
		strncpy(word_buf,s1+strlen(s1)-len,len);    //Take the final len bytes's word
		int n = WordDic.FindWord(word_buf);         // find word_buf word in WordDic	
		while(len > 3 && n == 0)                    //not find word_buf and len >3,one Chinese characters has three Byte
		{
			len -= 3; 
			memset(word_buf,0,ini_len);
			strncpy(word_buf,s1+strlen(s1)-len,len);//if the not find,then remove the first Chinese characters of the word
			n = WordDic.FindWord(word_buf);         //find word_buf word in WordDic	
		}	
		/*
		*if the word_buf can find in WordDic, then insert the word_buf into the map_nodes map
		*/
		if (n==1)
		{
			WordsMapStr(word_buf,mailmap);
		}
		strncpy(word_buf,s1,strlen(s1)-len);        //discard the lastest len char
		
		if((strlen(s1)-len)!=0)
		{
			strcpy(s1,word_buf);
		}
		else
		{	
			memset(s1,0,strlen(s1));
		}
	}
	free(s1);
	free(word_buf);
}

void SegmentSentenceMM (char *str,map<string,int>mapword[],int map_nodes)
{
	int i;
	int dd;
	int ini_len=strlen(str);
	char *s1=(char *)malloc(ini_len+1);
	strcpy(s1,str);
	char * tmp=(char *)malloc(ini_len+1);

	while(strlen(s1) != 0)
	{
		unsigned char ch = (unsigned char)s1[0];
		if (ch < 128)                               // English punctuation
		{
			i = 1;
			dd = strlen(s1);
			while (i < dd && ((unsigned char)s1[i] < 128) && (s1[i] != 10) && (s1[i] != 13)) //LR ,CF.Line breaks and spaces
			{
				i++;
			}
			strncpy(tmp,s1+i,dd);
			strcpy(s1,tmp);
			continue;
		}
		else
		{
			if (ch < 176)                           // chinese punctuation
			{
				i = 0;
				dd = strlen(s1);
				while(i < dd && ((unsigned char)s1[i] < 176) && ((unsigned char)s1[i] >= 161)
					&& (!((unsigned char)s1[i] == 161 && ((unsigned char)s1[i+1] >= 162 && (unsigned char)s1[i+1] <= 168)))
					&& (!((unsigned char)s1[i] == 161 && ((unsigned char)s1[i+1] >= 171 && (unsigned char)s1[i+1] <= 191)))
					&& (!((unsigned char)s1[i] == 163 && ((unsigned char)s1[i+1] == 172 || (unsigned char)s1[i+1] == 161)
					|| (unsigned char)s1[i+1] == 168 || (unsigned char)s1[i+1] == 169 || (unsigned char)s1[i+1] == 186
					|| (unsigned char)s1[i+1] == 187 || (unsigned char)s1[i+1] == 191)))
				{
					i = i + 3;                      // Each character three bytes
				}
				if (i == 0)
				{
					i = i +3;
				}
				strncpy(tmp,s1+i,dd);
				strcpy(s1,tmp);
				continue;
			}
		}
		i = 3;
		dd = strlen(s1);
		while(i < dd && (unsigned char)s1[i] >= 176)
		{
			i += 3;                                 //the length of s1 string with out punctuation marks
		}
		memset(tmp,0,ini_len+1);
		memcpy(tmp,s1,i);                           // s1 string removes the punctuation marks
		SegmentSentence(tmp,mapword,map_nodes);
		strncpy(tmp,s1+i,dd);                       //s1 string move backward
		strcpy(s1,tmp);
	}
	free(s1);
	free(tmp);
}

void SegmentSentenceStrMM (char *str,map<string,int>&mailmap)
{
	int i;
	int dd;
	int ini_len=strlen(str);
	char *s1=(char *)malloc(ini_len+1);
	strcpy(s1,str);
	char * tmp=(char *)malloc(ini_len+1);

	while(strlen(s1) != 0)
	{
		unsigned char ch = (unsigned char)s1[0];
		if (ch < 128)                               // English punctuation
		{
			i = 1;
			dd = strlen(s1);
			while (i < dd && ((unsigned char)s1[i] < 128) && (s1[i] != 10) && (s1[i] != 13)) //LR ,CF.Line breaks and spaces
			{
				i++;
			}
			strncpy(tmp,s1+i,dd);
			strcpy(s1,tmp);
			continue;
		}
		else
		{
			if (ch < 176)                           // chinese punctuation
			{
				i = 0;
				dd = strlen(s1);
				while(i < dd && ((unsigned char)s1[i] < 176) && ((unsigned char)s1[i] >= 161)
					&& (!((unsigned char)s1[i] == 161 && ((unsigned char)s1[i+1] >= 162 && (unsigned char)s1[i+1] <= 168)))
					&& (!((unsigned char)s1[i] == 161 && ((unsigned char)s1[i+1] >= 171 && (unsigned char)s1[i+1] <= 191)))
					&& (!((unsigned char)s1[i] == 163 && ((unsigned char)s1[i+1] == 172 || (unsigned char)s1[i+1] == 161)
					|| (unsigned char)s1[i+1] == 168 || (unsigned char)s1[i+1] == 169 || (unsigned char)s1[i+1] == 186
					|| (unsigned char)s1[i+1] == 187 || (unsigned char)s1[i+1] == 191)))
				{
					i = i + 3;                      // Each character three bytes
				}
				if (i == 0)
				{
					i = i +3;
				}
				strncpy(tmp,s1+i,dd);
				strcpy(s1,tmp);
				continue;
			}
		}
		i = 3;
		dd = strlen(s1);
		while(i < dd && (unsigned char)s1[i] >= 176)
		{
			i += 3;                                 //the length of s1 string with out punctuation marks
		}
		memset(tmp,0,ini_len+1);
		memcpy(tmp,s1,i);                           // s1 string removes the punctuation marks
		SegmentSentenceStr(tmp,mailmap);
		strncpy(tmp,s1+i,dd);                       //s1 string move backward
		strcpy(s1,tmp);
	}
	free(s1);
	free(tmp);
}

